/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.Mobilecontract;
import domain.Userdom;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Alejandro
 */
public class MobileContractDao {
    EntityManager em;
    EntityTransaction tx;

    public MobileContractDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Mobilecontract getMobileContract(Integer idMobileContract) {
        Mobilecontract m = (Mobilecontract) em.createNativeQuery("select * from mobilecontract as n where n.idMobileContract='" + idMobileContract + "'", Mobilecontract.class).getSingleResult();
        return m;
    }

    public void saveMobileContract(Mobilecontract m) {
        tx.begin();
        em.persist(m);
        tx.commit();
    }

    public void removeMobileContract(Mobilecontract m) {
        tx.begin();
        em.remove(m);
        tx.commit();
    }

    public List<Mobilecontract> getMobileContrList(Userdom user) {
        tx.begin();
        List list = em.createNativeQuery("select * from bankaccount b, contract c, mobilecontract m where b.userFK='"+user.getNick()+"' and c.bankFK=b.idBank and m.contractFK=c.idContract", Mobilecontract.class).getResultList();
        tx.commit();
        return list;
    }

    public void updateMobileContract(Mobilecontract m) {
        tx.begin();
        em.createNativeQuery("update mobilecontract set  company='"+m.getCompany()+"',permanence='"+m.getPermanence()+"', mobileNumber='"+m.getMobileNumber()+"' where idMobileContract='"+m.getIdMobileContract()+"'").executeUpdate();
        tx.commit();
    }
}